package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.common.utils.HexUtils;


/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class Data97 extends Data {

    private String ConnectionStatus;
    private String DisabledStatus;
    private String Voltage;
    private String Percentage;
    private String Charging;

    public Data97() {
    }

    public String getBasicStatus(JSONObject jsonObject) {
        Integer connectionStatus = jsonObject.getInteger("ConnectionStatus");
        Integer disabledStatus = jsonObject.getInteger("DisabledStatus");
        Integer total = disabledStatus * 2 + connectionStatus;
        String str = HexUtils.intToHex(total, 2);
        return str;
    }

    public String getVoltage(JSONObject jsonObject) {
        Integer voltage = jsonObject.getInteger("Voltage");
        return HexUtils.intToHex(voltage, 4);
    }

    public String getPercentage(JSONObject jsonObject) {
        Integer percentage = jsonObject.getInteger("Percentage");
        return HexUtils.intToHex(percentage, 2);
    }

    public String getCharging(JSONObject jsonObject) {
        Integer charging = jsonObject.getInteger("Charging");
        return HexUtils.intToHex(charging, 2);
    }

    public String getDataID() {
        // data97
        return HexUtils.intToHex(97, 2);
    }

    public String getLength() {
        return "16";
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append(this.getBasicStatus(jsonObject));
        sb.append(this.getVoltage(jsonObject));
        sb.append(this.getPercentage(jsonObject));
        sb.append(this.getCharging(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }

}
